﻿/// <copyright file="PreferencesDialog.cs" company="not for commercial use">
/// Copyright (c) 7/4/2012 All Right Reserved
/// </copyright>
/// <author>Dimitry Kaplin</author>
/// <date>7/4/2012 15:25 </date>
/// <summary>Dialog containig preferences</summary>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EthernetTools.UI.Dialogs
{
    public partial class PreferencesDialog : Form
    {
        public PreferencesDialog()
        {
            InitializeComponent();
        }

        #region Events

        private void button_save_and_exit_Click(object sender, EventArgs e)
        {
            Options.Global.ReadBufferSize = (uint)numericUpDown_buffersize.Value;
            Options.Global.Architecture = radioButton_x64.Checked ? "x64" : (radioButton_x86.Checked ? "x86" : "");
            Options.Global.LoadPreviousACsOnStart = checkBox_loadAcMap.Checked;
            Options.Global.DialTimeOut = (uint)numericUpDown_dialTimeOut.Value;
            Options.Global.PadoSpeedLimit = (int)numericUpDown_PadoSpeedLimit.Value;

            int padtMethod = 0;
            if (comboBox_hostPadt.SelectedIndex == 0)
            {
                padtMethod |= (int)Options.Global.PadtMethod.HostNone;
            }
            else if (comboBox_hostPadt.SelectedIndex == 1)
            {
                padtMethod |= (int)Options.Global.PadtMethod.HostDirect;
            }
            else if (comboBox_hostPadt.SelectedIndex == 2)
            {
                padtMethod |= (int)Options.Global.PadtMethod.HostBroadcast;
            }

            if (comboBox_ac_padt.SelectedIndex == 0)
            {
                padtMethod |= (int)Options.Global.PadtMethod.ConcentratorNone;
            }
            else if (comboBox_ac_padt.SelectedIndex == 1)
            {
                padtMethod |= (int)Options.Global.PadtMethod.ConcentratorDirect;
            }
            else if (comboBox_ac_padt.SelectedIndex == 2)
            {
                 padtMethod |= (int)Options.Global.PadtMethod.ConcentratorBroadcat;
            }
            Options.Global.CurrentPadtMethod = padtMethod;

            this.Close();
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        private void PreferencesDialog_Load(object sender, EventArgs e)
        {
            checkBox_loadAcMap.Checked = Options.Global.LoadPreviousACsOnStart;
            numericUpDown_buffersize.Value = Options.Global.ReadBufferSize;
            numericUpDown_dialTimeOut.Value = Options.Global.DialTimeOut;
            numericUpDown_PadoSpeedLimit.Value = Options.Global.PadoSpeedLimit;

            if ((Options.Global.CurrentPadtMethod & (int)Options.Global.PadtMethod.HostNone) != 0)
            {
                comboBox_hostPadt.SelectedIndex = 0;
            }
            else if ((Options.Global.CurrentPadtMethod & (int)Options.Global.PadtMethod.HostDirect) != 0)
            {
                comboBox_hostPadt.SelectedIndex = 1;
            }
            else if ((Options.Global.CurrentPadtMethod & (int)Options.Global.PadtMethod.HostBroadcast) != 0)
            {
                comboBox_hostPadt.SelectedIndex = 2;
            }

            if ((Options.Global.CurrentPadtMethod & (int)Options.Global.PadtMethod.ConcentratorNone) != 0)
            {
                comboBox_ac_padt.SelectedIndex = 0;
            }
            else if ((Options.Global.CurrentPadtMethod & (int)Options.Global.PadtMethod.ConcentratorDirect) != 0)
            {
                comboBox_ac_padt.SelectedIndex = 1;
            }
            else if ((Options.Global.CurrentPadtMethod & (int)Options.Global.PadtMethod.ConcentratorBroadcat) != 0)
            {
                comboBox_ac_padt.SelectedIndex = 2;
            }

            try
            {
                if (Utils.SystemUtils.Is64BitOs())
                {
                    radioButton_x64.Checked = true;
                    radioButton_x86.Checked = false;
                }
                else
                {
                    radioButton_x64.Checked = false;
                    radioButton_x86.Checked = true;
                }
                groupBox_system_arch.Enabled = false;
            }
            catch (System.ComponentModel.Win32Exception)
            {
                groupBox_system_arch.Enabled = true;
                radioButton_x64.Checked = false;
                radioButton_x86.Checked = false;
            }
        }
    }
}
